
function delay(interval=1000) {
    return new Promise(resolve => {
        let timer = setTimeout(() =>{
            clearTimeout(timer)
            resolve()
        },interval)
    })
}

//  xlsx文件转为二进制文件
function readFile(file) {
    return new Promise(resolve => {
        let reader = new FileReader()
        // 将文件转化为二进制格式
        reader.readAsBinaryString(file)
        reader.onload = (file) => {
            // 记录了读取信息
            resolve(file.target.result)
        }
    })
}

function formatJSON (weet) {
    let list = {
        name: {
            value: '姓名',
            type: 'String'
        },
        sex: {
            value: '性别',
            type: 'Number',
            rule: function (item) {
                return item === '男'? 0 : 1
            }
        },
        address: {
            value: '地址',
            type: 'String'
        },
        type: {
            value: '类型',
            type: 'String'
        },
        weixin: {
            value: '微信',
            type: 'String'
        },
        QQ: {
            value: 'QQ',
            type: 'String'
        },
        phone: {
            value: '电话',
            type: 'String'
        },
        email: {
            value: '邮箱',
            type: 'String'
        }
    }
    return weet.map(items => {
        let obj = {}
        Object.keys(list).forEach(item => {
            let {value,type,rule} = list[item]
            let result = items[value]
            if (rule) {
                result = rule(result)
            }
            if (type === 'String') {
                result = String(result)
            } else if (type === 'Number') {
                result = Number(result)
            }
            obj[item] = result
        })
        return obj
    })
}

function formatWeet (JSON) {
    let list = {
        '编号': {
            value: 'id',
            type: 'String'
        },
        '姓名': {
            value: 'name',
            type: 'String'
        },
        '性别': {
            value: 'sex',
            type: 'String',
            rule: function (item) {
                return item === 0 ? '男' : '女'
            }
        },
        '地址': {
            value: 'address',
            type: 'String'
        },
        '类型': {
            value: 'type',
            type: 'String'
        },
        '微信': {
            value: 'weixin',
            type: 'String'
        },
        'QQ': {
            value: 'QQ',
            type: 'String'
        },
        '电话': {
            value: 'phone',
            type: 'String'
        },
        '邮箱': {
            value: 'email',
            type: 'String'
        }
    }
    return JSON.map(items => {
        let obj = {}
        Object.keys(list).forEach(item => {
            let {value,type,rule} = list[item]
            let result = items[value]
            if (rule) {
                result = rule(result)
            }
            if (type === 'String') {
                result = String(result)
            } else if (type === 'Number') {
                result = Number(result)
            }

            obj[item] = result
        })
        return obj
    })
}


// 格式化事件函数
 function dataFormat(time){
    return `${time.getFullYear()}-${time.getMonth() + 1 >= 10 ? (time.getMonth() + 1) : '0' + (time.getMonth() + 1)}-${time.getDate() >= 10 ? time.getDate() : '0' + time.getDate()}`;
                      // ${time.getHours() >= 10 ? time.getHours() : '0' + time.getHours()} : ${time.getMinutes()>=10?time.getMinutes():'0'+time.getMinutes()} : ${time.getSeconds() >= 10 ? time.getSeconds() : '0' + time.getSeconds()}
}


export default {
    delay,
    readFile,
    formatJSON,
    formatWeet,
    dataFormat
}
